/**
 * 
 */
package com.wolfpire.proms.dao.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wolfpire.proms.dao.FeedBackDao;
import com.wolfpire.proms.dao.TaskDao;
import com.wolfpire.proms.model.FeedBack;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;

/**
 * @author lihd
 *
 */
@Repository("feedBackDao")
public class FeedBackDaoImpl extends BaseHibernateDao<FeedBack, Long> implements
		FeedBackDao {
	
	@Resource private TaskDao taskDao;

	@Override
	public Page<FeedBack> findFeedBacks(Page<FeedBack> page,
			FeedBack filterFeedBack) {
		Criterion[] criterions = this.createFilter(filterFeedBack);
		int totalCount = this.findIntByCriteria(criterions);
		page.setTotalCount(totalCount);
		
		if (0 < totalCount) {
			Criteria c = this.createCriteria(criterions);
			c.addOrder(Order.desc("id"));
			c = this.setPageParameter(c, page);
			@SuppressWarnings("unchecked")
			List<FeedBack> result = c.list();
			page.setDataList(result);
		}
		return page;
	}
	
	@Override
	public Page<FeedBack> findFeedBacks(EasyuiPage<FeedBack> page,
			FeedBack filterFeedBack, List<Order> orders) {
		Criterion[] criterions = this.createFilter(filterFeedBack);
		
		int totalCount = this.findIntByCriteria(criterions);
		page.setTotalCount(totalCount);
		if (0 < totalCount) {
			Criteria c = this.createCriteria(criterions);
			if (CollectionUtils.isNotEmpty(orders)) {
				for (Order o : orders) {
					c.addOrder(o);
				}
			} else {
				c.addOrder(Order.desc("id"));
			}
			c = this.setPageParameter(c, page);
			@SuppressWarnings("unchecked")
			List<FeedBack> result = c.list();
			page.setDataList(result);
		}
		return page;
	}

	private Criterion[] createFilter(FeedBack filterFeedBack) {
		if (null == filterFeedBack) {
			return null;
		}
		List<Criterion> criterions = new ArrayList<Criterion>();
		if (null != filterFeedBack.getId()) {
			criterions.add(Restrictions.eq("id", filterFeedBack.getId()));
		}
		if (!StringUtils.isBlank(filterFeedBack.getName())) {
			criterions.add(Restrictions.like("name", filterFeedBack.getName(), MatchMode.START));
		}
		if (!StringUtils.isBlank(filterFeedBack.getRemark())) {
			criterions.add(Restrictions.like("remark", filterFeedBack.getRemark(), MatchMode.ANYWHERE));
		}
		if (null != filterFeedBack.getTaskId()) {
			criterions.add(Restrictions.eq("taskId", filterFeedBack.getTaskId()));
		}
		if (null != filterFeedBack.getProjectIndexId()) {
			List<Long> taskIds = this.taskDao.getAllTaskIdsByProjectIndexId(filterFeedBack.getProjectIndexId());
			criterions.add(Restrictions.in("taskId", taskIds));
		}
		if (null != filterFeedBack.getCreateUserId()) {
			criterions.add(Restrictions.eq("createUserId", filterFeedBack.getCreateUserId()));
		}
		if (null != filterFeedBack.getDelFlag()) {
			criterions.add(Restrictions.eq("delFlag", filterFeedBack.getDelFlag()));
		}
		if (null != filterFeedBack.getStatus()) {
			criterions.add(Restrictions.eq("status", filterFeedBack.getStatus()));
		}
		return criterions.toArray(new Criterion[criterions.size()]);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FeedBack> list(FeedBack feedBack) {
		Criterion[] criterions = this.createFilter(feedBack);
		Criteria c = this.createCriteria(criterions);
		return c.list();
	}

	@Override
	public String countFactAmountByTaskId(Long taskId) {
		StringBuffer querySql = new StringBuffer();
		querySql.append(" SELECT SUM(IFNULL(fact_amount, '0.00') + 0.00) FROM t_feedback WHERE task_id=?  AND del_flag=? ");
		Object rs = this.findUniqueBySql(querySql.toString(), new Object[]{taskId, Constants.NORMAL_FLAG});
		if (null != rs) {
			BigDecimal value = new BigDecimal(rs.toString()).setScale(2, 1);
			return value.toString();
		}
		return "0.00";
	}

	@Override
	public String findLastProgressByTaskId(Long taskId) {
		StringBuffer querySql = new StringBuffer();
		querySql.append(" SELECT IFNULL(progress, '0.00') FROM t_feedback WHERE task_id=? AND del_flag=? ORDER BY create_time DESC LIMIT 1 ");
		Object rs = this.findUniqueBySql(querySql.toString(), new Object[]{taskId, Constants.NORMAL_FLAG});
		if (null != rs) {
			return rs.toString();
		}
		return "0.00";
	}

}
